package com.amlyc.lambda;

import java.nio.ByteBuffer;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class StreamDemo {
    public static void main(String[] args) {
       /* System.out.println("-----------------");
        Stream.iterate(new int[]{0,1}, t -> new int[]{t[1],t[0]+t[1]})
                .limit(20)
                .map(t->t[0])
                .forEach(System.out::println);
        System.out.println("-----------------");
        IntStream ones = IntStream.generate(() -> 1);*/
        LocalDate of = LocalDate.of(2014, 2, 12);
        System.out.println(of.getDayOfMonth());
        System.out.println(of.getDayOfYear());
        System.out.println(LocalDate.now());
        LocalDateTime ldt = LocalDateTime.of(2014, 11, 13, 21, 23, 22);
        System.out.println(ldt.toLocalDate());
        System.out.println(ldt.toLocalTime());
        System.out.println(Instant.now());
    }
}
class Shop {
    private String name;

    public Shop(String name) {
        this.name = name;
    }

    public double getPrice(String product) {
        return calculatePrice(product);
    }

    public Future<Double> getPriceAsync(String product) {
        CompletableFuture<Double> futurePrice = new CompletableFuture<>();
        new Thread(() ->{
            double price = calculatePrice(product);
            futurePrice.complete(price);
        }).start();
        return futurePrice;
    }

    public static void main(String[] args) {
        List<Shop> shops = Arrays.asList(new Shop("BestPrice"),
                new Shop("LetsSaveBig"),
                new Shop("MyFavoriteShop"),
                new Shop("BuyItAll"));


    }
    private double calculatePrice(String product) {
        Random random = new Random();
        delay();
        return random.nextDouble() * product.charAt(0) + product.charAt(1);
    }

    public static void delay() {
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
